package com.webchat.rmi.pay;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.domain.dto.payment.PaymentOrderCreateDTO;
import com.webchat.domain.dto.payment.PaymentOrderDTO;
import com.webchat.domain.dto.payment.PaymentResultDTO;
import com.webchat.domain.dto.payment.PaymentTransRequestDTO;
import com.webchat.domain.dto.payment.PaymentTransferRequestDTO;
import com.webchat.rmi.interceptor.RpcRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import java.math.BigDecimal;

@FeignClient(name = "webchat-pay-service",
             contextId = "paymentApiServiceClient",
             configuration = RpcRequestInterceptor.class)
public interface PaymentApiServiceClient {

    /**
     * 获取交易接入方凭证
     *
     * @param appId
     * @param accessKey
     * @param secretKey
     * @param timestamp
     * @param signature
     * @param logId
     * @return
     */
    @GetMapping("/pay-service/api/token")
    APIResponseBean<String> accessToken(@RequestHeader(name = "app-id") Long appId,
                                  @RequestHeader(name = "access-key") String accessKey,
                                  @RequestHeader(name = "secret-key") String secretKey,
                                  @RequestHeader Long timestamp,
                                  @RequestHeader String signature,
                                  @RequestHeader(name = "log-id") String logId);

    /**
     * 获取交易事务id（用于交易回滚，分布式事务一致性）
     *
     * @param token
     * @return
     */
    @GetMapping("/pay-service/api/transId")
    APIResponseBean<String> transId(@RequestHeader(name = "access-token") String token,
                                    @RequestHeader(name = "log-id") String logId);


    /**
     * 创建订单，获取订单id
     * @return
     */
    @PostMapping("/pay-service/api/orderId")
    APIResponseBean<String> orderId(@RequestHeader(name = "access-token") String token,
                                    @RequestHeader(name = "trans-id") String transId,
                                    @RequestHeader(name = "log-id") String logId,
                                    @RequestBody PaymentOrderCreateDTO paymentOrderCreateDTO);

    /**
     * 真实交易接口
     *
     * @return
     */
    @PostMapping("/pay-service/api/doTrans")
    APIResponseBean<Boolean> doTrans(@RequestBody PaymentTransRequestDTO paymentTransRequest,
                                     @RequestHeader(name = "access-token") String token,
                                     @RequestHeader(name = "log-id") String logId);

    /**
     * 真实交易接口, 支持回调
     *
     * @return
     */
    @PostMapping("/pay-service/api/trans")
    APIResponseBean<Boolean> trans(@RequestBody PaymentTransRequestDTO paymentTransRequest,
                                     @RequestHeader(name = "access-token") String token,
                                     @RequestHeader(name = "log-id") String logId);


    /**
     * 三方支付接口
     *
     * 支持callback
     *
     * @param paymentTransferRequest
     * @param token
     * @param logId
     * @return
     */
    @PostMapping("/pay-service/api/transfer")
    APIResponseBean<PaymentResultDTO> transfer(@RequestBody PaymentTransferRequestDTO paymentTransferRequest,
                                               @RequestHeader(name = "access-token") String token,
                                               @RequestHeader(name = "log-id") String logId);


    /**
     * 查询订单详情
     *
     * @param orderId
     * @param token
     * @return
     */
    @GetMapping("/pay-service/api/orderInfo/{orderId}/{userId}")
    APIResponseBean<PaymentOrderDTO> orderInfo(
            @PathVariable String orderId,
            @PathVariable String userId,
            @RequestHeader(name = "access-token") String token);

    /**
     * 交易回滚（保证分布式事务数据一致性）
     *
     * @param token
     * @param transId
     * @param logId
     * @return
     */
    @PostMapping("/pay-service/api/rollback")
    APIResponseBean<Boolean> rollback(@RequestHeader(name = "access-token") String token,
                                      @RequestHeader(name = "trans-id") String transId,
                                      @RequestHeader(name = "log-id") String logId);

    /**
     * 查询账户余额
     *
     * @param account
     * @return
     */
    @GetMapping("/pay-service/api/balance/{account}")
    APIResponseBean<BigDecimal> balance(@PathVariable String account,
                                        @RequestHeader(name = "access-token") String token,
                                        @RequestHeader(name = "log-id") String logId);
}
